$(document).ready(function (){
	view_init();
	event_init();
});

var url_param_id = getQueryString("id");

function view_init()
{
	loadingStart();
//	$("[data-type=dateAll]").each(function (){
//		$(this).datetimepicker({
//			  timeFormat: "HH:mm:ss",
//	          dateFormat: "yy-mm-dd",
//			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
//		});
//	});
 	if (url_param_id)
 	{
 		um_ajax_post(
 				"/childrenbooks/select.do",
 				{"id":url_param_id},
 				function (data){
 					//alert("c");
 					if (data && data.childrenbooks)
 					{
 	 					$("#main-content").umDataBind("render" ,data.childrenbooks[0] ,"");
 	 					$("#bookImageUrl").next().show();
 	 					$("#bookImageUrl").next().attr("src" ,data.childrenbooks[0].bookImageUrl);
 	 					$("#thumbnailUrl").next().show();
 	 					$("#thumbnailUrl").next().attr("src" ,data.childrenbooks[0].thumbnailUrl);
 					}

 				},
 				null,false);
 	}
 	
	loadingEnd();
	$("#main-content").show();
}

function event_init()
{
    $("#edit").click(function (){
    	childrenbooks_edit();
    });
    
	// 返回按钮
	$("#back").click(function (){
		window.location.href = "/childrenbooks/direction/index.do";
	});
	
	$("#homePageChk").click(function (){
		if ($(this).is(":checked"))
		{
			$("#homePageDiv").show();
		}
		else
		{
			$("#homePageDiv").hide();
		}
	});
	
	// 列表图片
    $("#bookImageUrl_img").dmUploader({
        url: INDEX_WEBAPP + '/childrenbooks/image/upload.do',
        dataType: 'json',
        allowedTypes: 'image/*',
        extraData: {data:JsonTools.encode({articleId:getQueryString("id")})},
        onUploadSuccess: function(id, data){
        	index_show("上传成功");
        	$("#bookImageUrl").val(data.result_map.url);
        	$("#bookImageUrl").next().attr("src" ,data.result_map.url);
        	$("#bookImageUrl").next().show();
        	$("#thumbnailUrl").val(data.result_map.thumbnailurl);
        	$("#thumbnailUrl").next().attr("src" ,data.result_map.thumbnailurl);
        	$("#thumbnailUrl").next().show();
        }
    });
    
}

function childrenbooks_edit()
{
	if (!$("#main-content").validate())
	{
		return false;
	}
	
	var startTime = $("[data-id=startTime]").val();
	var endTime = $("[data-id=endTime]").val();
	if (startTime && endTime)
	{
		if (moment(startTime).isBefore(moment()))
		{
			$("body").umDialog("alert" ,{"content":"报名开始时间不能小于当前时间"});
			return false;
		}
		
		if (moment(startTime).isAfter(endTime))
		{
			$("body").umDialog("alert" ,{"content":"报名开始时间不能大于结束时间"});
			return false;
		}
	}
	
	var acStartTime = $("[data-id=acStartTime]").val();
	var acEndTime = $("[data-id=acEndTime]").val();
	if (acStartTime && acEndTime)
	{
		if (moment(acStartTime).isBefore(moment()))
		{
			$("body").umDialog("alert" ,{"content":"活动开始时间不能小于当前时间"});
			return false;
		}
		
		if (moment(acStartTime).isAfter(acEndTime))
		{
			$("body").umDialog("alert" ,{"content":"活动开始时间不能大于活动结束时间"});
			return false;
		}
	}
	
	var obj = $("#main-content").umDataBind("serialize" ,"");
	var url = "/childrenbooks/add.do";

	if (url_param_id)
	{
		url = "/childrenbooks/update.do";
		obj.id = url_param_id;
	}
	
	if ($("#homePageChk").is(":checked"))
	{
		obj.isHomePage = 1;
	}
	obj.bookType = 1;//少儿图书
	um_ajax_post(
			url,
			obj,
			function (data){
				window.location.href = "/childrenbooks/direction/index.do?flag=" + SUCCESS;
			},
			null);
}